package com.random.fox.whatspay.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.view.View;

/**
 * Created by 渠 on 2014/8/29.
 */
public class LoadingLine extends View {
    final long times = 10*1000l;
    int count = 0;
    private Context mContext;
    public LoadingLine(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public Handler getHandler() {
        return super.getHandler();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        canvas.drawRect(0,0,currentWidth(),5,paint);
    }

    public void start(){
        new Thread(){
            @Override
            public void run() {
                while (count!=10){
                    try {
                        sleep(times/10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        count++;
                    }
                    Handler handler = getHandler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            invalidate();
                        }
                    });
                }
            }
        }.start();
    }

    private int currentWidth(){
        return getWidth()*count/10;
    }
}
